#import "LoginViewController.h"
#import "PPTextfield.h"
#import "LoginUtils.h"
#import "BaseWebViewViewController.h"
#import "CodeViewController.h"
#import "LoginUtils.h"
#import "BindMobileController.h"

@interface LoginViewController ()<UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet PPTextfield *accountTF;
///协议选择btn
@property (weak, nonatomic) IBOutlet UIButton *selectBtn;
///登录按钮
@property (weak, nonatomic) IBOutlet UIButton *loginBtn;

// 微信登录按钮
@property (weak, nonatomic) IBOutlet UIButton *btnWechat;
// 第三方提示登录Label
@property (weak, nonatomic) IBOutlet UILabel *thirdLoginLabel;

@end


@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
        self.thirdLoginLabel.hidden = !AppDelegate.shared.tuic;
        self.btnWechat.hidden = !AppDelegate.shared.tuic;
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [self.navigationController setNavigationBarHidden:YES animated: YES];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    
    [self.navigationController setNavigationBarHidden:YES animated:YES];
}


- (void)setUpSubViews{
    [self setAccountTF];
}

- (void)setAccountTF{
    self.view.backgroundColor = WHITECOLOR;
    
    CGRect paddingRect = {0, 0, 20, 30};
    UIView *leftView = [[UIView alloc] initWithFrame:paddingRect];
    leftView.backgroundColor = [UIColor clearColor];
    self.accountTF.leftView = leftView;
    self.accountTF.leftViewMode = UITextFieldViewModeAlways;
    
    UIView *rightView = [[UIView alloc] initWithFrame:paddingRect];
    rightView.backgroundColor = [UIColor clearColor];
    self.accountTF.rightView = rightView;
    self.accountTF.rightViewMode = UITextFieldViewModeAlways;
    
    self.accountTF.delegate = self;
    self.accountTF.isPhoneNumber = YES;
    self.loginBtn.enabled = NO;
    self.loginBtn.alpha = 0.4;
    
    WeakSelf(self)
    self.accountTF.ppTextfieldTextChangedBlock = ^(PPTextfield *tf) {
        StrongSelf(weakSelf)
        if (tf.text.length == 0) {
            strongSelf.loginBtn.enabled = NO;
            strongSelf.loginBtn.alpha = 0.4;
        } else {
            strongSelf.loginBtn.enabled = YES;
            strongSelf.loginBtn.alpha = 1.0;
        }
    };
}

- (IBAction)goback:(UIButton *)sender {
    [self.presentingViewController dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark 协议
- (IBAction)selectClick:(UIButton *)sender {
    sender.selected = !sender.selected;
}

#pragma mark 登录点击
- (IBAction)loginClick:(id)sender {
    NSString *phone = self.accountTF.text;
    
    if ([Utils isEmpty:phone]) {
        [self mbShowToast:@"手机号不能为空"];
        return;
    }
    
    if (!phone.hhe_isMobileNumber) {
        [self mbShowToast:@"请输入正确的手机号码"];
        return;
    }
    
    if (!self.selectBtn.selected) {
        [self mbShowToast:@"请阅读并同意TimeBoat《服务协议》及《隐私协议》"];
        [self.accountTF endEditing:YES];
        return;
    }

    CodeViewController *codeCtr = [[CodeViewController alloc] init];
    codeCtr.telephone = phone;
    [self.navigationController pushViewController:codeCtr animated:YES];
}

#pragma mark 微信登录
- (IBAction)wechatLoginClick:(UIButton *)sender {
    if (!self.selectBtn.selected) {
        return [self mbShowToast:@"请阅读并同意Time Boat《服务协议》及《隐私协议》"];
    }
    
    WeakSelf(self)
    [LoginUtils userLoginWithWX:self];
    [LoginUtils shareManager].callback = ^(NSDictionary * _Nonnull sender) {
        StrongSelf(weakSelf)
        [strongSelf weChatLoginStatueWithCode:sender[@"code"]];
    };
}

- (void)weChatLoginStatueWithCode:(NSString *)sender{
    NSMutableDictionary *dic = [NSMutableDictionary new];
    [dic safeSetObject:sender forKey:@"code"];
    [[HttpHelp shareInstance] POSTHTTPV2:API_LOGIN_weChatInfo parameters:dic success:^(NSDictionary * _Nonnull dict) {
        NSInteger result = [dict[@"result"] integerValue];
        if (result == 1) {
            [self login:dict[@"data"]];
        }
        
    } failure:^(BOOL netErr, NSString * _Nonnull error) {
        
    }];
}

#pragma mark - 用户登录
- (void)login:(NSDictionary *)dict{
    //loginType登录类型, 1-手机号码/2-微信
    NSMutableDictionary *parameter = @{
        @"avatarUrl":dict[@"headImgUrl"],
        @"code":@"",
        @"loginType":@"2",
        @"nickname":dict[@"nickname"],
        @"openId":dict[@"openId"],
        @"phone":@""
    }.mutableCopy;
    
    [LoginUtils userLoginWithParam:parameter block:^(LoginModel * _Nonnull model) {
        if (model) {
            [[AccountManager shareAccount] login:model];
            if (model.phone.length <= 0) {
                UIViewController *vc = [BindMobileController controller];
                vc.hidesBottomBarWhenPushed = YES;
                [self.navigationController pushViewController:vc animated:YES];
            } else{
                [APPDELEGATE configTab];
            }
        }
    } failure:^(NSString * _Nonnull error) {
        DMLog(@"%@", error);
    }];
}

- (IBAction)accountProtocalClick:(id)sender {
    BaseWebViewViewController *tempVC = [[BaseWebViewViewController alloc] init];
    tempVC.titleStr = @"服务协议";
    [tempVC initWebViewWithURLStr:ServiceAgreement];
    
    [self.navigationController pushViewController:tempVC animated:YES];
}


- (IBAction)privateProtocalClick:(id)sender {
    BaseWebViewViewController *tempVC = [[BaseWebViewViewController alloc] init];
    tempVC.titleStr = @"隐私协议";
    [tempVC initWebViewWithURLStr:PrivacyPolicy];
    [self.navigationController pushViewController:tempVC animated:YES];
}

@end
